﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK19.DB;

namespace PTTK19.GUI
{
    public partial class LapPhieuYeuCau : Form
    {
        bool init = false;
        int _curID = 0;
        List<Point> _dschitiet = new List<Point>();
        public LapPhieuYeuCau()
        {
            InitializeComponent();
        }

        private void LapPhieuYeuCau_Load(object sender, EventArgs e)
        {
            LoadCbxMaPhieu();
            txtMaPhieu.Text = "---";
            DateTime dt = DateTime.Now;
            txtNgayLap.Text = dt.ToShortDateString();
            cbxLoaiPhieu.SelectedIndex = 0;
        }

        private void LoadCbxMaPhieu()
        {
            DataTable dt = DBThucDon.GetListID();
            cbxMaThucDon.DataSource = dt;
            cbxMaThucDon.ValueMember = "MaThucDon";
            cbxMaThucDon.SelectedIndex = 0;
            init = true;
        }

        private void btnXemThucDon_Click(object sender, EventArgs e)
        {

        }

        private void LoadDgvMonAnID(int ID)
        {
            DataTable dt = DBMonAn.GetList(ID);
            dgvMonAn.DataSource = dt;
        }


        private void cbxMaThucDon_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                _dschitiet = new List<Point>();
                string dt = cbxMaThucDon.SelectedValue.ToString();
                int id = int.Parse(dt);
                LoadDgvMonAnID(id);
            }
            catch (Exception)
            {
            }
            
        }

        private void dgvMonAn_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = dgvMonAn.CurrentRow.Index;
                if (dgvMonAn.CurrentRow != null)
                {
                    //lay ma mon an
                    _curID = int.Parse(dgvMonAn.Rows[index].Cells[0].Value.ToString());
                    txtTenMonAn.Text = (string)dgvMonAn.Rows[index].Cells[1].Value;
                    for (int i = 0; i < _dschitiet.Count; i++)
                    {
                        if (_dschitiet[i].X == _curID)
                        {
                            txtSoLuong.Text = _dschitiet[i].Y.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

            bool flag = false;
            for (int i = 0; i < _dschitiet.Count; i++)
            {
                if (_dschitiet[i].X == _curID)
                {
                    _dschitiet.RemoveAt(i);
                    Point pX = new Point();
                    pX.X = _curID;
                    pX.Y = int.Parse(txtSoLuong.Text);
                    _dschitiet.Add(pX);
                    flag = true;
                }
            }
            if (!flag)
            {
                Point pX = new Point();
                pX.X = _curID;
                pX.Y = int.Parse(txtSoLuong.Text);
                _dschitiet.Add(pX);
            }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txtSoLuong, "Không được nhập chữ");
            }
        }

        private void btnGuiYeuCau_Click(object sender, EventArgs e)
        {
            if(_dschitiet.Count != 0)
            {
                int idx = cbxLoaiPhieu.SelectedIndex;
                bool kq = DBPhieuYeuCau.GuiYeuCau(idx, _dschitiet);
            }
        }
    }
}
